package net.caixiaomi.info.ui.dialog;

import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.DisplayMetrics;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;

import net.caixiaomi.info.R;
import net.caixiaomi.info.adapter.ElementCellAdapter;
import net.caixiaomi.info.base.BaseDialogFragment;
import net.caixiaomi.info.base.quickadapter.BaseQuickAdapter;
import net.caixiaomi.info.model.CommonEvent;
import net.caixiaomi.info.model.ElementCellModel;
import net.caixiaomi.info.model.MatchPlaysList;
import net.caixiaomi.info.model.PlayFootballItem;
import net.caixiaomi.info.util.EventWhat;
import net.caixiaomi.info.widgets.GridDivider;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static android.R.id.list;

/**
 * Created by mengxianzheng on 2018/4/14.
 */

public class HalfAndAllDialogFragment extends BaseDialogFragment implements BaseQuickAdapter.OnItemClickListener {
    @BindView(R.id.host)
    TextView mHost;
    @BindView(R.id.guest)
    TextView mGuest;

    @BindView(R.id.text)
    TextView mText;
    @BindView(list)
    RecyclerView mListView;

    private PlayFootballItem mPlayFootBallItem;
    private ElementCellAdapter mAdapter;

    @OnClick(android.R.id.button1)
    void toConfirm() {
        try {
            String text = JSON.toJSONString(mPlayFootBallItem);
            EventBus.getDefault().post(new CommonEvent(EventWhat.EVENT_FOOTBALL_SELECT_DONE, text));
            dismiss();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @OnClick(android.R.id.button2)
    void toCancel() {
        dismiss();
    }

    @Override
    protected int layoutRes() {
        return R.layout.dialog_half_and_all;
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        try {
            String strData = getArguments().getString("data");
            mPlayFootBallItem = JSON.parseObject(strData, new TypeReference<PlayFootballItem>() {
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void onViewCreated(@NonNull View view) {
        try {
            Window window = getDialog().getWindow();
            WindowManager.LayoutParams wlp = window.getAttributes();
            wlp.width = ViewGroup.LayoutParams.MATCH_PARENT;
            wlp.height = ViewGroup.LayoutParams.WRAP_CONTENT;
            wlp.gravity = Gravity.BOTTOM;
            window.setAttributes(wlp);
            DisplayMetrics dm = new DisplayMetrics();
            getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
            window.setBackgroundDrawable(new ColorDrawable(0xff000000));
            getDialog().getWindow().setLayout(dm.widthPixels, getDialog().getWindow().getAttributes().height);
            mHost.setText(mPlayFootBallItem.getHomeTeamAbbr());
            mGuest.setText(mPlayFootBallItem.getVisitingTeamAbbr());
            StringBuilder stringBuilder = new StringBuilder();
            String string = getString(R.string.C_HALF_AND_ALL);
            for (int index = 0; index < string.length(); index++) {
                stringBuilder.append(string.charAt(index));
                if (index != string.length() - 1) {
                    stringBuilder.append("\n");
                }
            }
            mText.setText(stringBuilder);
            mText.setBackgroundColor(Color.parseColor("#EB6D8E"));

            MatchPlaysList matchPlaysList = mPlayFootBallItem.getMatchPlays().get(0);
            if (mPlayFootBallItem.getSelectCell() != null) {
                mPlayFootBallItem.getSelectCell().clear();
            }
            List<ElementCellModel> matchCells = matchPlaysList.getMatchCells();
            for (ElementCellModel cellModel : matchCells) {
                if (cellModel.isSelected()) {
                    mPlayFootBallItem.getSelectCell().add(cellModel);
                }
            }
            mListView.setLayoutManager(new GridLayoutManager(getActivity(), 3));
            mListView.addItemDecoration(new GridDivider(getActivity(), (int) getResources().getDimension(R.dimen.divider),  ContextCompat.getColor(getActivity(),R.color.light_gray)));
            mListView.setNestedScrollingEnabled(false);
            mAdapter = new ElementCellAdapter(R.layout.view_textview);
            mAdapter.setOnItemClickListener(this);
            mAdapter.setNewData(matchCells);
            mAdapter.bindToRecyclerView(mListView);
            getDialog().setCanceledOnTouchOutside(true);
            getDialog().setCancelable(true);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
        try {
            if (mPlayFootBallItem.getSelectCell() == null) {
                mPlayFootBallItem.setSelectCell(new ArrayList<ElementCellModel>());
            }
            ElementCellModel cellModel = mAdapter.getData().get(position);
            cellModel.setPlayType(mPlayFootBallItem.getPlayType());
            cellModel.setSelected(!cellModel.isSelected());
            if (cellModel.isSelected()) {
                mPlayFootBallItem.getSelectCell().add(cellModel);
            } else {
                mPlayFootBallItem.getSelectCell().remove(cellModel);
            }
            mAdapter.notifyDataSetChanged();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mPlayFootBallItem = null;
        mAdapter = null;
    }
}
